local ctld = CTLD:New(SIDE.BLUE, { "BLUE" }, "Viking")
ctld:__Start(5)

-- ========
-- troops
-- ========

-- Infantry
ctld:AddTroopsCargo("Infantry", { "Blue Inf TEMPLATE" }, CTLD_CARGO.Enum.TROOPS, 12)

-- Anti-Air
ctld:AddTroopsCargo("AA group", { "Blue InfAA TEMPLATE" }, CTLD_CARGO.Enum.Troops, 4)

-- engineers
ctld:AddTroopsCargo("Wrenches", { "Engineers" }, CTLD_CARGO.Enum.ENGINEERS, 4)
ctld.EngineerSearch = 2000

-- ========
-- Crates
-- ========

-- Tank
ctld:AddCratesCargo("MBT",
    { "MBT Template-1", "MBT Template-2", "MBT Template-3" },
    CTLD_CARGO.Enum.VEHICLE, 2
)

-- APC
ctld:AddCratesCargo("APC",
    { "APC Template-1", "APC Template-2", "APC Template-3", "APC Template-4" },
    CTLD_CARGO.Enum.VEHICLE, 1
)

-- repair
ctld:AddCratesRepair("MBT repair", "Repair", CTLD_CARGO.Enum.REPAIR, 1)
ctld.repairtime = 30

SendMessageAll("CTLD loading...")
-- ========
-- Zones
-- ========

-- loadzones
ctld:AddCTLDZone("LOADZONE-1", CTLD.CargoZoneType.LOAD, SMOKECOLOR.Blue, true, true)
ctld:AddCTLDZone("LOADZONE-2", CTLD.CargoZoneType.LOAD, SMOKECOLOR.Blue, true, true)

-- dropzones
ctld:AddCTLDZone("DROPZONE-1", CTLD.CargoZoneType.DROP, SMOKECOLOR.Green, true, true)

-- movezones
ctld:AddCTLDZone("MOVEZONE-1", CTLD.CargoZoneType.MOVE, SMOKECOLOR.Blue, true, true)

-- ========
-- config
-- ========
ctld.useprefix = true

ctld.useprefix = true                -- (DO NOT SWITCH THIS OFF UNLESS YOU KNOW WHAT YOU ARE DOING!) Adjust **before** starting CTLD. If set to false, *all* choppers of the coalition side will be enabled for CTLD.
ctld.CrateDistance = 35              -- List and Load crates in this radius only.
ctld.dropcratesanywhere = false      -- Option to allow crates to be dropped anywhere.
ctld.dropAsCargoCrate = false        -- Parachuted herc cargo is not unpacked automatically but placed as crate to be unpacked. Needs a cargo with the same name defined like the cargo that was dropped.
ctld.maximumHoverHeight = 15         -- Hover max this high to load.
ctld.minimumHoverHeight = 4          -- Hover min this low to load.
ctld.forcehoverload = false          -- Crates (not: troops) can **only** be loaded while hovering.
ctld.hoverautoloading = true         -- Crates in CrateDistance in a LOAD zone will be loaded automatically if space allows.
ctld.smokedistance = 2000            -- Smoke or flares can be request for zones this far away (in meters).
ctld.movetroopstowpzone = true       -- Troops and vehicles will move to the nearest MOVE zone...
ctld.movetroopsdistance = 5000       -- .. but only if this far away (in meters)
ctld.smokedistance = 2000            -- Only smoke or flare zones if requesting player unit is this far away (in meters)
ctld.suppressmessages = false        -- Set to true if you want to script your own messages.
ctld.repairtime = 30                -- Number of seconds it takes to repair a unit.
ctld.buildtime = 10                 -- Number of seconds it takes to build a unit. Set to zero or nil to build instantly.
ctld.cratecountry = country.id
    .GERMANY                         -- ID of crates. Will default to country.id.RUSSIA for RED coalition setups.
ctld.allowcratepickupagain = true    -- allow re-pickup crates that were dropped.
ctld.enableslingload = false         -- allow cargos to be slingloaded - might not work for all cargo types
ctld.pilotmustopendoors = false      -- force opening of doors
ctld.SmokeColor = SMOKECOLOR
    .Red                             -- default color to use when dropping smoke from heli
ctld.FlareColor = FLARECOLOR
    .Red                             -- color to use when flaring from heli
ctld.basetype =
"container_cargo"                    -- default shape of the cargo container
ctld.droppedbeacontimeout = 600      -- dropped beacon lasts 10 minutes
ctld.usesubcats = false              -- use sub-category names for crates, adds an extra menu layer in "Get Crates", useful if you have > 10 crate types.
ctld.placeCratesAhead = false        -- place crates straight ahead of the helicopter, in a random way. If true, crates are more neatly sorted.
ctld.nobuildinloadzones = true       -- forbid players to build stuff in LOAD zones if set to `true`
ctld.movecratesbeforebuild = true    -- crates must be moved once before they can be build. Set to false for direct builds.
ctld.surfacetypes = { land.SurfaceType.LAND, land.SurfaceType.ROAD, land.SurfaceType.RUNWAY,
    land.SurfaceType.SHALLOW_WATER } -- surfaces for loading back objects.
ctld.nobuildmenu = false             -- if set to true effectively enforces to have engineers build/repair stuff for you.

-- hercules
ctld.enableHercules = true
ctld.HercMinAngels = 155  -- for troop/cargo drop via chute in meters, ca 470 ft
ctld.HercMaxAngels = 2000 -- for troop/cargo drop via chute in meters, ca 6000 ft
ctld.HercMaxSpeed = 77    -- 77mps or 270kph or 150kn

-- ========
-- FOB
-- ========
ctld:AddCratesCargo("FARP", { "FOB" }, CTLD_CARGO.Enum.FOB, 2)

-- FARP Radio. First one has 130AM, next 131 and for forth
local FARPFreq = 130
local FARPName = 1 -- numbers 1..10

local FARPClearnames = {
    [1] = "London",
    [2] = "Dallas",
    [3] = "Paris",
    [4] = "Moscow",
    [5] = "Berlin",
    [6] = "Rome",
    [7] = "Madrid",
    [8] = "Warsaw",
    [9] = "Dublin",
    [10] = "Perth",
}

function BuildAFARP(Coordinate)
    local coord = Coordinate -- Core.Point#COORDINATE

    local FarpName = ((FARPName - 1) % 10) + 1
    local FName = FARPClearnames[FarpName]

    FARPFreq = FARPFreq + 1
    FARPName = FARPName + 1

    -- Create a SPAWNSTATIC object from a template static FARP object.
    local SpawnStaticFarp = SPAWNSTATIC:NewFromStatic("BLUE FARP-2-1", country.id.USA)

    -- Spawning FARPs is special in DCS. Therefore, we need to specify that this is a FARP. We also set the callsign and the frequency.
    SpawnStaticFarp:InitFARP(FARPName, FARPFreq, 0)
    SpawnStaticFarp:InitDead(false)

    -- Spawn FARP
    local ZoneSpawn = ZONE_RADIUS:New("FARP " .. FName, Coordinate:GetVec2(), 160, false)
    local Heading = 0
    local FarpBerlin = SpawnStaticFarp:SpawnFromZone(ZoneSpawn, Heading, "FARP " .. FName)

    -- ATC and services - put them 125m from the center of the zone towards North
    local FarpVehicles = SPAWN:NewWithAlias("FARP Vehicles Template", "FARP " .. FName .. " Technicals")
    FarpVehicles:InitHeading(180)
    local FarpVCoord = coord:Translate(125, 0)
    FarpVehicles:SpawnFromCoordinate(FarpVCoord)

    -- We will put the rest of the statics in a nice circle around the center
    local base = 330
    local delta = 30

    local windsock = SPAWNSTATIC:NewFromStatic("静态 风向标-1-1", country.id.USA)
    local sockcoord = coord:Translate(125, base)
    windsock:SpawnFromCoordinate(sockcoord, Heading, "Windsock " .. FName)
    base = base - delta

    local fueldepot = SPAWNSTATIC:NewFromStatic("静态 补给点油库-1", country.id.USA)
    local fuelcoord = coord:Translate(125, base)
    fueldepot:SpawnFromCoordinate(fuelcoord, Heading, "Fueldepot " .. FName)
    base = base - delta

    local ammodepot = SPAWNSTATIC:NewFromStatic("静态 补给点弹药仓库-1", country.id.USA)
    local ammocoord = coord:Translate(125, base)
    ammodepot:SpawnFromCoordinate(ammocoord, Heading, "Ammodepot " .. FName)
    base = base - delta

    local CommandPost = SPAWNSTATIC:NewFromStatic("静态 补给点掩体指挥所-1", country.id.USA)
    local CommandCoord = coord:Translate(125, base)
    CommandPost:SpawnFromCoordinate(CommandCoord, Heading, "Command Post " .. FName)
    base = base - delta

    local Tent1 = SPAWNSTATIC:NewFromStatic("静态 补给点帐篷-1", country.id.USA)
    local Tent1Coord = coord:Translate(125, base)
    Tent1:SpawnFromCoordinate(Tent1Coord, Heading, "Command Tent " .. FName)
    base = base - delta

    local Tent2 = SPAWNSTATIC:NewFromStatic("静态 补给点帐篷-1", country.id.USA)
    local Tent2Coord = coord:Translate(125, base)
    Tent2:SpawnFromCoordinate(Tent2Coord, Heading, "Command Tent2 " .. FName)

    -- add a loadzone to CTLD
    ctld:AddCTLDZone("FARP " .. FName, CTLD.CargoZoneType.LOAD, SMOKECOLOR.Blue, true, true)
    local m = MESSAGE:New(string.format("FARP %s in operation!", FName), 15, "CTLD"):ToBlue()
end

function ctld:OnAfterCratesBuild(From, Event, To, Group, Unit, Vehicle)
    local name = Vehicle:GetName()
    if string.match(name, "FOB", 1, true) then
        local Coord = Vehicle:GetCoordinate()
        Vehicle:Destroy(false)
        BuildAFARP(Coord)
    end
end
